package com.wanxi.springboot.team.manage.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.wanxi.springboot.team.manage.system.api.CommonResult;
import com.wanxi.springboot.team.manage.system.generator.WordGenerate;
import com.wanxi.springboot.team.manage.system.model.DailySummary;
import com.wanxi.springboot.team.manage.system.mapper.DailySummaryMapper;
import com.wanxi.springboot.team.manage.system.service.DailySummaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@Service
public class DailySummaryServiceImpl extends ServiceImpl<DailySummaryMapper, DailySummary> implements DailySummaryService {

    @Autowired(required = false)
    DailySummaryMapper dailySummaryMapper;

    @Value("${export.filePath}")
    private String filePath;

    @Override
    public CommonResult addSummary(DailySummary dailySummary) {
        String str = dailySummary.getPublishStatus();
        String msg = "";
        try {
            int result = dailySummaryMapper.addSummary(dailySummary);
            if (str.equals("draft"))
                msg = "已保存为草稿";
            else if (str.equals("published"))
                msg = "日报已提交";
            return CommonResult.success(result,msg);
        }catch (Exception e){
            e.printStackTrace();
            if (str.equals("draft"))
                msg = "保存草稿失败";
            else if (str.equals("published"))
                msg = "日报提交失败";
            return CommonResult.failed(msg);
        }

    }

    @Override
    public CommonResult getDailySummary(Map map) {
        int page = (Integer.parseInt((String) map.get("page")) - 1) * Integer.parseInt((String) map.get("limit"));
        map.put("page", page);
        map.put("limit", Integer.parseInt((String) map.get("limit")));
        List<DailySummary> dailySummary = dailySummaryMapper.getDailySummary(map);
        long count = dailySummaryMapper.getDailySummaryCount(map);
        return CommonResult.success(dailySummary,count);
    }

    @Override
    public CommonResult editSummary(DailySummary dailySummary) {
        String str = dailySummary.getPublishStatus();
        String msg = "";
        try {
            int result = dailySummaryMapper.editSummary(dailySummary);
            if (str.equals("draft"))
                msg = "已保存为草稿";
            else if (str.equals("published"))
                msg = "日报已提交";
            return CommonResult.success(result,msg);
        }catch (Exception e){
            e.printStackTrace();
            if (str.equals("draft"))
                msg = "保存草稿失败";
            else if (str.equals("published"))
                msg = "日报提交失败";
            return CommonResult.failed(msg);
        }
    }

    @Override
    public CommonResult readSummary(Integer id) {
        try {
            int result = dailySummaryMapper.readSummary(id);
            return CommonResult.success(result,"成功批阅日报");
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("日报批阅失败");
        }
    }

    @Override
    public CommonResult getSelfDailySummary(Map map) {
        int page = (Integer.parseInt((String) map.get("page")) - 1) * Integer.parseInt((String) map.get("limit"));
        map.put("page", page);
        map.put("limit", Integer.parseInt((String) map.get("limit")));
        List<DailySummary> dailySummary = dailySummaryMapper.getSelfDailySummary(map);
        long count = dailySummaryMapper.getSelfDailySummaryCount(map);
        return CommonResult.success(dailySummary,count);
    }

    @Override
    public CommonResult exportSummary(Map map, HttpServletResponse response) {
        String date = (String) map.get("date");
        String publisher = (String) map.get("publisher");
        String fileName = publisher+date+"日报.doc";
        try {
            WordGenerate.WordGenerate(map,filePath,fileName,response);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("导出时遇到错误");
        }
        return CommonResult.success(true,"导出成功,请在 "+filePath+" 文件夹下查看名为:"+fileName+" 的文件");
    }

    @Override
    public CommonResult batchExportSummary(String list, HttpServletResponse response) {
        List<Map> maps = JSONArray.parseArray(list, Map.class);
        for(Map map :maps) {
            String date = (String) map.get("date");
            String publisher = (String) map.get("publisher");
            String fileName = publisher + date + "日报.doc";
            try {
                WordGenerate.WordGenerate(map, filePath, fileName, response);
            } catch (Exception e) {
                e.printStackTrace();
                return CommonResult.failed("导出时遇到错误");
            }
        }
        return CommonResult.success(true,"导出成功,请在 "+filePath+" 文件夹下查看");
    }
}
